home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADVANCED / videoresize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  15.7 KB  |  612 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1996. */
  3.  
  4. /* This program is freely distributable without licensing fees  and is
  5.    provided without guarantee or warrantee expressed or  implied. This
  6.    program is -not- in the public domain. */
  7.  
  8. /* This demo shows off InfiniteReality's "dynamic video resize" (DVR)
  9.    capability.  Dynamic video resizing let's you maintain a constant frame
  10.    rate even when the view becomes limited by your hardware's pixel fill
  11.    rate.  The idea is simple:  Draw the fill-limited frame into a smaller
  12.    area (ie, touch less pixels) and then have the video hardware "zoom" up
  13.    the image to fill the video screen.  This trick does require special
  14.    hardware.
  15.  
  16.    As written with no command line options, this demo works well on a 1RM
  17.    InfiniteReality.  It keeps a scene containing colored large orbs that
  18.    rotate around the viewer at a constant 60 frames/sec, even though the
  19.    number of viewable orbs is changing (hence the required fullscreen pixel
  20.    fill rate is changing too).  The demo adapts the rendered image size and
  21.    video resizing based on the last frame to keep a constant frame rate. */
  22.  
  23. /****************************************************************
  24.  
  25. Command line arguments:
  26.  
  27.   -window ... run in a window instead of full screen and will not use
  28.      hardware resizing even if the hardware supports it.  Good for
  29.      demos.
  30.  
  31.   -novidresize ... start without using video resize hardware even though
  32.      the system might really support video resizing.
  33.  
  34.   -debug ... output frame size to stdout.
  35.  
  36.   -target # ... sets the target frame rate in increments of 60th of
  37.       a second; the default is 1.
  38.  
  39.   -twosnaps ... sample the last frame rate as the average of the last
  40.       two frames; the default is to simply use the last frame.
  41.  
  42.   -nice ... better tesselate the orbs; nice if you have transformation
  43.       rate to burn (ie, use an InfiniteReality).
  44.  
  45.   -orbs # ... an initial number of orbs to populate the viewing space;
  46.       the default is 80.
  47.  
  48. Example command lines:
  49.  
  50.   Because this program's behavior depends on adapting to the speed of the
  51.   system it is running at, a good demonstration of this program may depend
  52.   on the system performance.  Adjust the command line options accordingly,
  53.   particularly -target and -orbs.
  54.  
  55.   InfiniteReality (1RM) ... "videoresize"
  56.  
  57.     (For the hardware listed below, you won't get actual video resizing
  58.      since the hardware lacks the support.)
  59.  
  60.   Indigo^2 XL 200Mhz ... "videoresize -geometry 800x600 -target 5 -orbs 40 -window"
  61.  
  62. Key controls:
  63.  
  64.   ESC or q ... exit the demo.
  65.  
  66.   n ... disable dynamic video resizing.
  67.  
  68.   r ... enable dynamic video resizing (the default if hardware video
  69.      resizing is detected; otherwise no video resizing is the
  70.      default). 
  71.      
  72.      NOTE: you can enable dynamic video resizing even without the
  73.      hardware and the demo will show you how the rendered area would
  74.      vary as if the feature were supported.
  75.  
  76.   h ... toggle use of hardware video resizing (only if in fullscreen
  77.      mode and the hardware supports the feature).
  78.  
  79.   m ... toggle display of the frame meter.  Enabled by default.
  80.  
  81.   c ... toggle display of the cursor.  Sometimes it is nice to see the
  82.      cursor when video resizing so that you can see the effect of the
  83.      video resizing since the cursor resizes too, but in general a
  84.      resizing cursor is quite distracting.  The cursor is enabled if
  85.      hardware video resizing is to be used.
  86.  
  87.   Up arrow ... add 10 more orbs.
  88.  
  89.   Down arrow ... subtract 10 less orbs.
  90.  
  91.   Spacebar ... regenerate the set of viewing orbs.
  92.  
  93.   Right arrow ... increase rightward rotation.
  94.  
  95.   Left arrow ... increase rightward rotation.
  96.  
  97. Frame meter:
  98.  
  99.   The frame meter shows how long each frame takes to render.  It
  100.   operates differently depending on if video resizing is being
  101.   demonstrated or not.
  102.  
  103.   The meter line is BLUE when video resizing is not being demonstrated.
  104.  
  105.   When video resizing is being demonstrated, a GREEN meter line
  106.   indicates the frame is being shown at full (non-resized) resolution.
  107.   YELLOW indicates the frame is being video resized.  RED indicates the
  108.   frame is being resized so much that dropping a frame is better than
  109.   the resulting poor resize quality (the demo won't zoom anymore than 8
  110.   to 1).  A smaller MAGENTA line shows a calculation of the
  111.   "unadjusted" frame rate for the displayed scene.  When the MAGENTA
  112.   line is longer than the base line, it is showing a relative measure
  113.   of how much resizing was needed to keep the frame at a sustained
  114.   frame rate.
  115.  
  116. Bugs:
  117.  
  118.   This program assumes a frame rate of 60 Hz.
  119.  
  120.   This program should use InfiniteReality's SGIX_instruments
  121.   extension for better fill rate measurement.
  122.  
  123. *****************************************************************/
  124.  
  125. #include <GL/glut.h>
  126. #include <stdlib.h>
  127. #include <stdio.h>
  128. #include <string.h>
  129. #include <math.h>
  130. #include <unistd.h>
  131.  
  132. #define OVERLOAD 0
  133. #define ZOOMED 1
  134. #define UNZOOMED 2
  135. #define STATIC 3
  136. GLfloat state_colors[4][3] =
  137. {
  138.   {1.0, 0.0, 0.0},
  139.   {0.8, 0.8, 0.0},
  140.   {0.0, 0.7, 0.0},
  141.   {0.3, 0.3, 0.8},
  142. };
  143. int state;
  144.  
  145. extern void sphere(int level);
  146.  
  147. GLdouble angle, speed = 0.5;
  148.  
  149. GLfloat light0_ambient[] =
  150. {0.1, 0.1, 0.1, 1.0};
  151. GLfloat light0_diffuse[] =
  152. {0.5, 0.5, 0.5, 1.0};
  153. GLfloat light0_position[] =
  154. {0.0, 0.0, 0.0, 1.0};
  155.  
  156. typedef struct {
  157.   GLfloat offset;
  158.   GLfloat distance;
  159.   GLfloat size;
  160.   GLfloat *color;
  161. } Orb;
  162.  
  163. Orb *orbs = NULL;
  164. int num_orbs = 80;      /* Good default for 1 RM InfiniteReality. */
  165. int fullscreen = 1;
  166. int debug = 0;
  167. int video_resizing = 1;
  168. int hw_video_resizing = 1;  /* Assume we have it until told otherwise. */
  169. int hw_exists = 0;
  170. int frame_time = 15;
  171. int sphere_quality = 1;
  172. int max_w, max_h;
  173. GLfloat W, H;
  174. int delta_w_resize = 1, delta_h_resize = 1;
  175. GLfloat target_frame_time = 1000.0 / 60.0;
  176. int show_cursor = 1, show_meter = 1;
  177.  
  178. #define NUM_SNAPS 2
  179. int snap[NUM_SNAPS] =
  180. {0};
  181. int num_snaps = 1;
  182. int frame = 0;
  183.  
  184. GLfloat color[][3] =
  185. {
  186.   {0.0, 0.5, 0.0},
  187.   {0.5, 0.5, 0.0},
  188.   {1.0, 0.0, 0.5},
  189.   {0.0, 1.0, 0.5},
  190.   {1.0, 0.0, 0.0},
  191.   {1.0, 0.0, 1.0},
  192.   {0.0, 0.0, 0.8},
  193.   {0.0, 0.2, 0.5},
  194.   {1.0, 1.0, 0.0},
  195.   {1.0, 1.0, 1.0},
  196.   {0.3, 0.3, 0.3},
  197. };
  198. int num_colors = sizeof(color) / (sizeof(GLfloat) * 3);
  199.  
  200. void
  201. calculate_frame_rate(int delta)
  202. {
  203.   int sum, count, i;
  204.  
  205.   snap[frame] = delta;
  206.   frame += 1;
  207.   frame %= num_snaps;
  208.  
  209.   sum = 0;
  210.   count = 0;
  211.   for (i = 0; i < num_snaps; i++) {
  212.     if (snap[i] != 0) {
  213.       sum += snap[i];
  214.       count++;
  215.     }
  216.   }
  217.   frame_time = sum / count;
  218. }
  219.  
  220. void
  221. advancedAnimation(void)
  222. {
  223.   angle = (GLfloat) fmod(angle + speed, 360.0);
  224.   glutPostRedisplay();
  225. }
  226.  
  227. void
  228. resize(int w, int h)
  229. {
  230.   glViewport(0, 0, w, h);
  231.   glMatrixMode(GL_PROJECTION);
  232.   glLoadIdentity();
  233.   gluPerspective(50.0, (GLfloat) w / (GLfloat) h, 1.0, 100.0);
  234.   glMatrixMode(GL_MODELVIEW);
  235. }
  236.  
  237. void
  238. reshape(int w, int h)
  239. {
  240.   max_w = w;
  241.   max_h = h;
  242.   W = w;
  243.   H = h;
  244.   resize(w, h);
  245. }
  246.  
  247. void
  248. do_video_resize_logic(void)
  249. {
  250.   GLfloat frame_ratio, adjusted_area, dimension_scale;
  251.   GLfloat area;
  252.   int Wint, Hint;
  253.  
  254.   if (frame_time > (target_frame_time * 0.92)) {
  255.     /* Shrink case. */
  256.     frame_ratio = (target_frame_time * 0.92) / frame_time;
  257.     area = W * H;
  258.     adjusted_area = area * frame_ratio;
  259.     dimension_scale = sqrt(adjusted_area) / sqrt(area);
  260.     W = dimension_scale * W;
  261.     H = dimension_scale * H;
  262.     if (W < max_w / 8 || H < max_h / 8) {
  263.       W = max_w / 8;
  264.       H = max_h / 8;
  265.       state = OVERLOAD;
  266.     } else {
  267.       state = ZOOMED;
  268.     }
  269.     Wint = (((int) W) / delta_w_resize) * delta_w_resize;
  270.     Hint = (((int) H) / delta_h_resize) * delta_h_resize;
  271.     if (video_resizing) {
  272.       if (hw_video_resizing)
  273.         glutVideoResize(0, 0, -Wint, -Hint);
  274.       resize(Wint, Hint);
  275.     }
  276.   } else if (frame_time < (target_frame_time * 0.82)) {
  277.     /* Grow case. */
  278.     frame_ratio = (target_frame_time * 0.82) / frame_time;
  279.     area = W * H;
  280.     adjusted_area = area * frame_ratio;
  281.     dimension_scale = sqrt(adjusted_area) / sqrt(area);
  282.  
  283.     /* Be a bit conservative about our growth.  As Allan Greenspan would say, 
  284.        "You don't want to overheat the economy; put on the brakes." */
  285.     dimension_scale = (dimension_scale - 1.0) * 0.75 + 1.0;
  286.     if (dimension_scale > 1.5) {
  287.       dimension_scale = 1.5;
  288.     }
  289.     W = dimension_scale * W;
  290.     H = dimension_scale * H;
  291.     if (W > max_w || H > max_h) {
  292.       W = max_w;
  293.       H = max_h;
  294.       state = UNZOOMED;
  295.     } else {
  296.       state = ZOOMED;
  297.     }
  298.     Wint = (((int) W) / delta_w_resize) * delta_w_resize;
  299.     Hint = (((int) H) / delta_h_resize) * delta_h_resize;
  300.     if (video_resizing) {
  301.       if (hw_video_resizing)
  302.         glutVideoResize(0, 0, -Wint, -Hint);
  303.       resize(Wint, Hint);
  304.     }
  305.   } else {
  306.     if (!hw_video_resizing) {
  307.       /* We keep the meter constant size, so we must keep restoring our
  308.          viewport. */
  309.       Wint = (((int) W) / delta_w_resize) * delta_w_resize;
  310.       Hint = (((int) H) / delta_h_resize) * delta_h_resize;
  311.       glViewport(0, 0, Wint, Hint);
  312.     }
  313.   }
  314.   if (debug)
  315.     printf("%gx%g\n\n", W, H);
  316. }
  317.  
  318. void
  319. display(void)
  320. {
  321.   int i, start, stop;
  322.  
  323.   if (video_resizing)
  324.     do_video_resize_logic();
  325.  
  326.   start = glutGet(GLUT_ELAPSED_TIME);
  327.  
  328.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  329.  
  330.   glEnable(GL_DEPTH_TEST);
  331.   glEnable(GL_LIGHTING);
  332.  
  333.   glMatrixMode(GL_MODELVIEW);
  334.   glLoadIdentity();
  335.   gluLookAt(0.0, 0.0, 1.0,
  336.     0.0, 0.0, 0.0,
  337.     0.0, 1.0, 0.0);
  338.   glTranslatef(0.0, 0.0, -1.0);
  339.  
  340.   for (i = 0; i < num_orbs; i++) {
  341.     glPushMatrix();
  342.     glRotatef(angle + orbs[i].offset, 0.0, 1.0, 0.0);
  343.     glTranslatef(0.0, 0.0, orbs[i].distance);
  344.     glScalef(orbs[i].size, orbs[i].size, orbs[i].size);
  345.     glColor3fv(orbs[i].color);
  346.     glCallList(1);
  347.     glPopMatrix();
  348.   }
  349.  
  350.   if (show_meter) {
  351.     if (!hw_video_resizing) {
  352.       /* Make sure the meter stays constant size in window mode. */
  353.       glViewport(0, 0, max_w, max_h);
  354.     }
  355.     glMatrixMode(GL_PROJECTION);
  356.     glPushMatrix();
  357.     glLoadIdentity();
  358.     gluOrtho2D(0, 3000, 0, 3000);
  359.     glMatrixMode(GL_MODELVIEW);
  360.     glPushMatrix();
  361.     glLoadIdentity();
  362.  
  363.     glDisable(GL_DEPTH_TEST);
  364.     glDisable(GL_LIGHTING);
  365.  
  366.     glColor3fv(state_colors[state]);
  367.     glRecti(100, 100, 100 + frame_time * 8, 200);
  368.     if (video_resizing) {
  369.       float render_area = W * H, full_area = max_w * max_h;
  370.       float advantage = sqrt(full_area / render_area);
  371.  
  372.       glColor3f(1.0, 0.0, 1.0);
  373.       glRecti(100, 140, 100 + frame_time * advantage * 8, 160);
  374.     }
  375.     glColor3f(1.0, 1.0, 1.0);
  376.     glBegin(GL_LINES);
  377.     for (i = 0; i < 21; i++) {
  378.       glVertex2f(100 + i * 16.6 * 8, 80);
  379.       glVertex2f(100 + i * 16.6 * 8, 220);
  380.     }
  381.     glEnd();
  382.  
  383.     glPopMatrix();
  384.     glMatrixMode(GL_PROJECTION);
  385.     glPopMatrix();
  386.   }
  387.   glFinish();
  388.   stop = glutGet(GLUT_ELAPSED_TIME);
  389.  
  390.   glutSwapBuffers();
  391.  
  392.   calculate_frame_rate(stop - start);
  393. }
  394.  
  395. void
  396. visible(int vis)
  397. {
  398.   if (vis == GLUT_VISIBLE)
  399.     glutIdleFunc(advancedAnimation);
  400.   else
  401.     glutIdleFunc(NULL);
  402. }
  403.  
  404. void
  405. set_cursor(int on)
  406. {
  407.   if (on) {
  408.     glutSetCursor(GLUT_CURSOR_INHERIT);
  409.   } else {
  410.     glutSetCursor(GLUT_CURSOR_NONE);
  411.   }
  412. }
  413.  
  414. void
  415. generate_orbs(void)
  416. {
  417.   int i, r, angle, chance, delta, d;
  418.  
  419.   if (orbs)
  420.     free(orbs);
  421.   orbs = (Orb *) malloc(num_orbs * sizeof(Orb));
  422.  
  423.   i = 0;
  424.   angle = 0;
  425.   while (i < num_orbs) {
  426.     angle += 37;
  427.     angle %= 360;
  428.     r = (int) lrand48();
  429.     chance = abs(r % 360 - 180);
  430.     delta = chance - abs(angle - 180);
  431.     if (delta > 0) {
  432.       orbs[i].color = color[lrand48() % num_colors];
  433.       orbs[i].offset = angle;
  434.       d = (r % 20) + (delta / 180.0) * 10;
  435.       orbs[i].distance = d + 3.0;
  436.       orbs[i].size = 1.4 * exp(d / 8.0) / exp(1.0);
  437.       i++;
  438.     }
  439.   }
  440. }
  441.  
  442. /* ARGSUSED */
  443. void
  444. keyboard(unsigned char k, int x, int y)
  445. {
  446.   switch (k) {
  447.   case 27:
  448.   case 'q':
  449.   case 'Q':
  450.     exit(0);
  451.     break;
  452.   case 'R':
  453.   case 'r':
  454.     video_resizing = 1;
  455.     break;
  456.   case 'H':
  457.   case 'h':
  458.     if (hw_exists) {
  459.       hw_video_resizing = 1 - hw_video_resizing;
  460.       if (!hw_video_resizing) {
  461.         glutVideoResize(0, 0, max_w, max_h);
  462.         resize(max_w, max_h);
  463.       }
  464.     }
  465.     break;
  466.   case 'N':
  467.   case 'n':
  468.     video_resizing = 0;
  469.     if (hw_video_resizing)
  470.       glutVideoResize(0, 0, max_w, max_h);
  471.     resize(max_w, max_h);
  472.     state = STATIC;
  473.     break;
  474.   case 'D':
  475.   case 'd':
  476.     debug = 1;
  477.     break;
  478.   case 'M':
  479.   case 'm':
  480.     show_meter = 1 - show_meter;
  481.     break;
  482.   case ' ':
  483.     generate_orbs();
  484.     break;
  485.   case 'C':
  486.   case 'c':
  487.     show_cursor = 1 - show_cursor;
  488.     set_cursor(show_cursor);
  489.     break;
  490.   }
  491. }
  492.  
  493. /* ARGSUSED */
  494. void
  495. special(int k, int x, int y)
  496. {
  497.   switch (k) {
  498.   case GLUT_KEY_RIGHT:
  499.     speed -= 0.25;
  500.     break;
  501.   case GLUT_KEY_LEFT:
  502.     speed += 0.25;
  503.     break;
  504.   case GLUT_KEY_UP:
  505.     num_orbs += 10;
  506.     generate_orbs();
  507.     break;
  508.   case GLUT_KEY_DOWN:
  509.     num_orbs -= 10;
  510.     generate_orbs();
  511.     break;
  512.   }
  513. }
  514.  
  515. int
  516. main(int argc, char **argv)
  517. {
  518.   int i;
  519.  
  520.   glutInit(&argc, argv);
  521.   for (i = 1; i < argc; i++) {
  522.     if (!strcmp(argv[i], "-orbs")) {
  523.       i++;
  524.       if (i >= argc) {
  525.         fprintf(stderr, "videoresize: need number of orbs\n");
  526.         exit(1);
  527.       }
  528.       num_orbs = atoi(argv[i]);
  529.       if (num_orbs < 1) {
  530.         fprintf(stderr, "videoresize: number of orbs must be 1 or more\n");
  531.         exit(1);
  532.       }
  533.     } else if (!strcmp(argv[i], "-target")) {
  534.       i++;
  535.       if (i >= argc) {
  536.         fprintf(stderr, "videoresize: need target number of frames\n");
  537.         exit(1);
  538.       }
  539.       target_frame_time = atoi(argv[i]) * 1000.0 / 60.0;
  540.       if (target_frame_time <= 0.0) {
  541.         fprintf(stderr, "videoresize: target frames must be 1 or more\n");
  542.         exit(1);
  543.       }
  544.     } else if (!strcmp(argv[i], "-novidresize")) {
  545.       hw_video_resizing = 0;
  546.     } else if (!strcmp(argv[i], "-debug")) {
  547.       debug = 1;
  548.     } else if (!strcmp(argv[i], "-window")) {
  549.       fullscreen = 0;
  550.     } else if (!strcmp(argv[i], "-twosnaps")) {
  551.       num_snaps = 2;
  552.     } else if (!strcmp(argv[i], "-nice")) {
  553.       sphere_quality = 2;
  554.     } else {
  555.       printf("usage: videoresize [-window] [-twosnaps] [-nice] [-target #] [-orbs #]\n");
  556.       printf("  -target #    = target number of frame intervals to stay under (default=%g)\n", target_frame_time);
  557.       printf("  -orbs #      = number of orbs to randomly position (default=%d)\n", num_orbs);
  558.       printf("  -window      = do not go fullscreen\n");
  559.       printf("  -nice        = use better tesselated spheres\n");
  560.       printf("  -novidresize = even if videoresizing is supported, don't use it\n");
  561.       printf("  -twosnaps    = averages over last two frames instead of just estimating\n");
  562.       printf("                 based on the last frame\n");
  563.       exit(1);
  564.     }
  565.   }
  566.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  567.   glutCreateWindow("Dynamic video resizing for constant frame rates");
  568.   if (fullscreen) {
  569.     glutFullScreen();
  570.     if (glutVideoResizeGet(GLUT_VIDEO_RESIZE_POSSIBLE)) {
  571.       delta_w_resize = glutVideoResizeGet(GLUT_VIDEO_RESIZE_WIDTH_DELTA);
  572.       delta_h_resize = glutVideoResizeGet(GLUT_VIDEO_RESIZE_HEIGHT_DELTA);
  573.       show_cursor = 0;
  574.       glutSetupVideoResizing();
  575.       hw_exists = 1;
  576.     } else {
  577.       hw_video_resizing = 0;
  578.     }
  579.   } else {
  580.     hw_video_resizing = 0;
  581.   }
  582.   if (video_resizing) {
  583.     state = UNZOOMED;
  584.   } else {
  585.     state = STATIC;
  586.   }
  587.   glutReshapeFunc(reshape);
  588.   glDisable(GL_CULL_FACE);  /* Makes us more fill limited. */
  589.   glEnable(GL_LIGHT0);
  590.   glColorMaterial(GL_FRONT, GL_DIFFUSE);
  591.   glEnable(GL_COLOR_MATERIAL);
  592.   glEnable(GL_NORMALIZE);
  593.   glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
  594.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  595.   glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  596.  
  597.   glNewList(1, GL_COMPILE);
  598.   sphere(sphere_quality);
  599.   glEndList();
  600.  
  601.   srand48(getpid() * 16 + glutGet(GLUT_ELAPSED_TIME));
  602.   generate_orbs();
  603.  
  604.   glutDisplayFunc(display);
  605.   glutVisibilityFunc(visible);
  606.   glutKeyboardFunc(keyboard);
  607.   glutSpecialFunc(special);
  608.   set_cursor(show_cursor);
  609.   glutMainLoop();
  610.   return 0;             /* ANSI C requires main to return int. */
  611. }
  612.